package com.stox.ui.javafx.list.application;

import javafx.scene.control.ContextMenu;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.dto.intf.AbstractModelNavigable;
import com.stox.ui.javafx.list.widget.menu.AddToWatchListMenu;
import com.stox.ui.javafx.list.widget.menu.WatchListContextMenu;

@Component
public class WatchListMediator implements BeanPostProcessor {

	@Autowired
	private ApplicationContext applicationContext;
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		if(bean instanceof ContextMenu && bean instanceof AbstractModelNavigable && !(bean instanceof WatchListContextMenu)){
			ContextMenu menu = (ContextMenu)bean;
			AddToWatchListMenu addToWatchListMenu = applicationContext.getBean(AddToWatchListMenu.class);
			menu.getItems().add(addToWatchListMenu);
		}
		return bean;
	}

}
